/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui;

import java.util.Calendar;

import utsc.apps.utschedule.util.TimetableHelper;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockDialogFragment;

/**
 * A dialog fragment that allows user to select a date. 
 *
 */
public class MyDatePickerDialogFragment extends SherlockDialogFragment {
	
	public static final String TAG="MyDatePickerDialog";
	
	private OnDateSetListener _datePickerCallBack;
	private int _year;
	private int _month;
	private int _day;
	private static final String ARGS_YEAR_KEY="year";
	private static final String ARGS_MONTH_KEY="month";
	private static final String ARGS_DAY_KEY="day";
	
	/**
	 * The recommended way of creating a new instance by taking in a callback
	 * listener, a pre-defined year, month, and day values.
	 * 
	 * @param listener
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static MyDatePickerDialogFragment newInstance(OnDateSetListener listener,int year,int month,int day){
		MyDatePickerDialogFragment fragment=new MyDatePickerDialogFragment(listener);
		Bundle args=new Bundle();
		args.putInt(ARGS_YEAR_KEY, year);
		args.putInt(ARGS_MONTH_KEY, month);
		args.putInt(ARGS_DAY_KEY, day);
		fragment.setArguments(args);
		return fragment;
	}
	
	public MyDatePickerDialogFragment(OnDateSetListener listener){
		this._datePickerCallBack=listener;
	}
	
	public MyDatePickerDialogFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args=getArguments();
		if (args!=null) {
			Calendar c=TimetableHelper.getCurrentDateCalendar();
			_year=args.getInt(ARGS_YEAR_KEY,c.get(Calendar.YEAR));
			_month=args.getInt(ARGS_MONTH_KEY,c.get(Calendar.MONTH));
			_day=args.getInt(ARGS_DAY_KEY,c.get(Calendar.DAY_OF_MONTH));
		}
	}
	
	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
		getDialog().dismiss();
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return new DatePickerDialog(getSherlockActivity(), _datePickerCallBack, _year, _month,_day);
	}
}
